package main

import "fmt"

//#link: https://leetcode-cn.com/problems/decode-xored-permutation/
func main() {
	encoded := []int{6, 5, 4, 6}
	fmt.Println(decode(encoded))
	nums := []int{10, 13, 8, 14, 5, 11, 4, 15, 9}
	fmt.Println(encode(nums))
}

func decode(encoded []int) []int {
	n := len(encoded) + 1
	allXor := 0
	for i := 1; i <= n; i++ {
		allXor ^= i
	}
	firstNum := 0
	for i := 1; i < n-1; i = i + 2 {
		firstNum ^= encoded[i]
	}
	result := make([]int, 0)
	result = append(result, firstNum^allXor)
	for i := 1; i < n; i++ {
		result = append(result, result[i-1]^encoded[i-1])
	}
	return result
}

func encode(nums []int) []int {
	result := make([]int, 0)
	for i := 0; i < len(nums)-1; i++ {
		result = append(result, nums[i]^nums[i+1])
	}
	return result
}
